package cn.stylefeng.guns.modular.manager.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.module.OrderStatus;
import cn.stylefeng.guns.modular.manager.entity.FlowOrder;
import cn.stylefeng.guns.modular.manager.mapper.FlowOrderMapper;
import cn.stylefeng.guns.modular.manager.model.params.FlowOrderParam;
import cn.stylefeng.guns.modular.manager.model.result.FlowOrderResult;
import  cn.stylefeng.guns.modular.manager.service.FlowOrderService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 订单记录 服务实现类
 * </p>
 *
 * @author yumker
 * @since 2021-01-07
 */
@Service
public class FlowOrderServiceImpl extends ServiceImpl<FlowOrderMapper, FlowOrder> implements FlowOrderService {

    @Override
    public void add(FlowOrderParam param){
        FlowOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FlowOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FlowOrderParam param){
        FlowOrder oldEntity = getOldEntity(param);
        FlowOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FlowOrderResult findBySpec(FlowOrderParam param){
        return null;
    }

    @Override
    public List<FlowOrderResult> findListBySpec(FlowOrderParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FlowOrderParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FlowOrderParam param){
        return param.getOrderId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FlowOrder getOldEntity(FlowOrderParam param) {
        return this.getById(getKey(param));
    }

    private FlowOrder getEntity(FlowOrderParam param) {
        FlowOrder entity = new FlowOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public String orderStatus(int orderStatus) {
        if(orderStatus == OrderStatus.OrderWaitConfirm){
            return "待处理";
        }else if(orderStatus == OrderStatus.OrderDealing){
            return "处理中";
        }else if(orderStatus == OrderStatus.OrderWaitPay){
            return "等待付款";
        }else if(orderStatus == OrderStatus.OrderWaitService){
            return "已支付";
        }else if(orderStatus == OrderStatus.OrderServiceSuccess){
            return "服务完成";
        }else if(orderStatus == OrderStatus.OrderCompletion){
            return "订单完成";
        }else if(orderStatus == OrderStatus.OrderCansel){
            return "关闭订单";
        }else {
            return "";
        }
    }

    @Override
    public int updateOrderStatus(Long orderId, int orderStatusNew, int orderStatusOld) {
        return baseMapper.updateOrderStatus(orderId,orderStatusNew,orderStatusOld);
    }
}
